// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Number of path to follow.
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;
short last_spawn;
short last_abil;
short r1;

body;

beginstate INIT_STATE;
	set_aggression(ME,8);
	set_walk_speed(ME,22);
	
	last_spawn = get_current_tick();
	last_abil = get_current_tick();
	
				place_particle_num(ME,18,12,4);		
				place_particle_num(ME,19,12,4);		
				place_particle_num(ME,20,12,4);		
	
	break;

beginstate DEAD_STATE;
	inc_flag(66,4,1);
break;

beginstate START_STATE;
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	follow_path(ME,get_memory_cell(0),1);


	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((tick_difference(last_spawn,get_current_tick()) > 2) && (get_attitude(ME) >= 10) && 
	  (get_nearest_party_char(8) >= 0)) {
		r1 = get_ran(1,0,2);
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (summon_creature(get_memory_cell(3 + r1) - 8)) {
				print_named_str(ME,"makes a new creation!");
				set_summon_level(get_memory_cell(3 + r1),1);
				place_particle_num(get_memory_cell(3 + r1),10,7,10);
				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				last_spawn = get_current_tick();
				end();
				}
			}
		}

	if ((get_ran(1,0,100) < 50) && (is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 2)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();

		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds itself with an aura of lightning.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,7,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,5),7,1,0);
			status_nearby(get_level(ME) / 4,7,20,0);


		end();
		}

	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(2) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(2));	
break;